/**
 * Created by meiguizhinian on 2018/6/28
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UIAniBox = ivy.UIBase.extend({
	_className:"ivy.UIAniBox",
	ctor : function(formName, blockName, sourceControlData) {
		this._super();
		this._animationID1 = new ivy.AnimationID();
		this._animationID2 = new ivy.AnimationID();
		this._animationID3 = new ivy.AnimationID();
		if (formName) {
			this.initWith(formName, blockName, sourceControlData, true);
		}
	},

	/**
	 * initWith(formID, blockID)
	 * initWith(formName, blockName, sourceControlData, trueType)
	 * */
	initWith : function(formNameOrID, blockNameOrID, sourceControlData, trueType=false) {
		if (!formNameOrID || !blockNameOrID) {
			return false;
		}
		if (cc.isString(formNameOrID) && cc.isString(blockNameOrID)) {
			let formName = formNameOrID;
			let blockName = blockNameOrID;
			this.setUIElementName(formName, blockName);
			let uim = ivy.UIManager.getInstance();
			this._aniIDInUI1 = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eBgAniID);
			this._actionIDInUI1 = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eBgActionID);
			this._seqIDInUI1 = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eBgSeqID);
			let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
			let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
			let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
			let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
			let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
			this._aniIDInUI2 = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eSelBgAniID);
			this._actionIDInUI2 = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eSelBgActionID);
			this._seqIDInUI2 = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eSelBgSeqID);
			let scaleX = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eCompressX);
			let scaleY = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eCompressY);
			let aniIDInUI3 = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eComeAniID);
			let actionIDInUI3 = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eComActionID);
			this._layerIDInEditor = layerID;

			if (this._aniIDInUI1 >= 0) {
				let aniFileName = ivy.UIManager.getInstance().getAniFileNameByUIAniID(this._aniIDInUI1);
				this._aniPlayer = ivy.AniPlayer.create(aniFileName, this._actionIDInUI1, this._seqIDInUI1===-1 ? 0:this._seqIDInUI1, true, this._seqIDInUI1===-1 ? ivy.AniPlayState.eCircle:ivy.AniPlayState.ePause);
				this._aniPlayer.setCascadeColorEnabled(true);
				this._aniPlayer.setCascadeOpacityEnabled(true);
				this.addChild(this._aniPlayer);

				let aniClassID = ivy.SceneCommonDataManager.getInstance().getClassIDByAniFileName(aniFileName);
				this._animationID1 = new ivy.AnimationID(aniClassID, this._actionIDInUI1);
			}
			if (this._aniIDInUI2 >= 0) {
				let aniFileName = ivy.UIManager.getInstance().getAniFileNameByUIAniID(this._aniIDInUI2);
				let aniClassID = ivy.SceneCommonDataManager.getInstance().getClassIDByAniFileName(aniFileName);
				this._animationID2 = new ivy.AnimationID(aniClassID, this._actionIDInUI2);
			}
			if (aniIDInUI3 >= 0) {
				let aniFileName = ivy.UIManager.getInstance().getAniFileNameByUIAniID(aniIDInUI3);
				let aniClassID = ivy.SceneCommonDataManager.getInstance().getClassIDByAniFileName(aniFileName);
				this._animationID3 = new ivy.AnimationID(aniClassID, actionIDInUI3);
			}

			this.setLocalZOrder(layerID);

			if (scaleX !== 100) {
				this._aniScaleX = scaleX/100.0;
				if(this._aniPlayer)
					this._aniPlayer.setScaleX(this._aniScaleX);
			}
			if (scaleY !== 100) {
				this._aniScaleY = scaleY/100.0;
				if (this._aniPlayer)
					this._aniPlayer.setScaleY(this._aniScaleY);
			}

			if(trueType) {
				this._playType = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniBox_PlayType_list]);
				this._setAniplayerPlayerState();

				let backLayerColor = uim.getControlColorDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniBox_CoverLayer_color]);
				let backLayeIsrFullScreen = true;
				if (sourceControlData.propertyDataFindIndex.length > ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniBox_CoverLayerFullScreen_list) {
					backLayeIsrFullScreen = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniBox_CoverLayerFullScreen_list]) === 1 ;
				}
				if (backLayerColor > 0) {
					this._backLayer = new cc.LayerColor(ivy.Tools.getColor4BFromInt32(backLayerColor));
					this._backLayer.setCascadeColorEnabled(true);
					this._backLayer.setCascadeOpacityEnabled(true);
					if(backLayeIsrFullScreen) {
						this._backLayer.setScale(10000);
					} else {
						this._backLayer.setContentSize(cc.size(w,h));
					}
					this.addChild(this._backLayer, -1);
				}
				let stt= uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniBox_SwallowTouchType_list]);
				if (stt !== ivy.UIAniBox.SwallowTouchType.eNone) {
					let listener = {
						event : cc.EventListener.TOUCH_ONE_BY_ONE,
						swallowTouches : true,
					};
					if (stt === ivy.UIAniBox.SwallowTouchType.eAniBoxSize) {
						listener.onTouchBegan = function(touch){
							if (!this.isVisible())
								return false;
							if (this.getOpacity() <= 0)
								return false;
							let touchPoint = this.convertTouchToNodeSpace(touch);
							let touchRect = this.getTouchRect();
							if (cc.rectContainsPoint(touchRect, touchPoint)) {
								if (this._touchEnterFunc) {
									this._touchEnterFunc(this);
								}
								return true;
							}
							return false;
						}.bind(this);

						listener.onTouchEnded = function(touch) {
							let touchPoint = this.convertTouchToNodeSpace(touch);
							let touchRect = this.getTouchRect();
							if (cc.rectContainsPoint(touchRect, touchPoint)) {
								if (this._touchEndFunc) {
									this._touchEndFunc(this);
								}
							}
						}.bind(this);
					} else if(stt===ivy.UIAniBox.SwallowTouchType.eAllScreen) {
						let swallowTouchOutZoneAction = uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniBox_SwallowTouchOutZoneClick_action]);
						listener.onTouchBegan = function(){
							if (!this.isVisible())
								return false;
							if (this._touchEnterFunc) {
								this._touchEnterFunc(this);
							}
							return true;
						}.bind(this);
						listener.onTouchEnded = function(touch) {
							let touchPoint = this.convertTouchToNodeSpace(touch);
							let touchRect = this.getTouchRect();
							if (this._touchEndFunc) {
								this._touchEndFunc(this);
							}
							if (!cc.rectContainsPoint(touchRect, touchPoint)) {
								if(swallowTouchOutZoneAction>=0) {
									this.runAction(ivy.CocosActionDataManager.getInstance().getGameActionBy(swallowTouchOutZoneAction));
								}
							}
						}.bind(this);
					}
					let listener1 = cc.EventListener.create(listener);
					this.setTouchListener(listener1);
					cc.eventManager.addListener(listener1, this);
					this.setSwallowTouches(true);
				}
			}
			this.setSrcRect(cc.rect(x, y, w, h));
			this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w/2.0, y + h/2.0)));
			this.setSrcPosition(this.getPosition());
			this.setContentSize(cc.size(w, h));
			let anchorType = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniBox_Anchor_list]);
			this.setAnchorType(anchorType);
			this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniBox_EnterAction_action]));
			this._outActionID .setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniBox_OutAction_action]));
			//////////////////////////////////////////////////////////////////////////
			//设置初始可视性 wsq-2017-3-7
			let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
			if (isNotVisble === 1){
				this.setVisible(false);
			}
		} else {
			let formID = formNameOrID;
			let blockID = blockNameOrID;
			let uim = ivy.UIManager.getInstance();
			let aniIDInUI = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eBgAniID);
			let actionIDInUI = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eBgActionID);
			let seqIDInUI = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eBgSeqID);
			let aniFileName = uim.getAniFileNameByUIAniID(aniIDInUI);
			this._layerIDInEditor = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eLayerID);
			this._aniPlayer = ivy.AniPlayer.create(aniFileName, actionIDInUI, seqIDInUI === -1 ? 0 : seqIDInUI, true, seqIDInUI === -1 ? ivy.AniPlayState.ePause : ivy.AniPlayState.eCircle);
			this.addChild(this._aniPlayer);
			let x = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.ePosX);
			let y = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.ePosY);
			let w = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eWidth);
			let h = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eHeight);
			this.setSrcRect(cc.rect(x,y,w,h));
			this.setPosition(cc.Tools.ConvertPointToGL(cc.p(x + w/2.0, y + h/2.0)));
			this.setContentSize(cc.size(w, h));
		}
	},
	/*
	 *  setAnimationID(aniID)
	 *  setAnimationID(aniFileName, actionID, sequenceID, selfUpdate, playState)
	 *  */
	setAnimationID : function(aniFileNameOrID, actionID, sequenceID, selfUpdate, playState) {
		let aniID = null;
		if (typeof(aniFileNameOrID)==="string") {
			let aniClassID = ivy.SceneCommonDataManager.getInstance().getClassIDByAniFileName(aniFileNameOrID);
			aniID = new ivy.AnimationID(aniClassID, actionID, sequenceID, selfUpdate, playState);
		} else {
			aniID = aniFileNameOrID;
		}

		if (this._aniPlayer && aniID.vaild()) {
			this._aniPlayer.resetAnimation(aniID);
		} else if (aniID.vaild()) {
			this._aniPlayer = ivy.AniPlayer.create(aniID);
			this._aniPlayer.setCascadeColorEnabled(true);
			this._aniPlayer.setCascadeOpacityEnabled(true);
			this.addChild(this._aniPlayer);
			this.setAnchorType(this._anchorType);
		}
	},
	setAnchorType : function(type) {
		////UIBase::setAnchorType(type); =>
		this._super(type);
		let rect;
		let size;
		if (this.getParent() == null){
			rect = cc.rect(this.getSrcRect());
			size = cc.size(rect.width, rect.height);
		} else {
			rect = cc.rect(this.getSrcRect());
			size = cc.size(rect.width, rect.height);
			let parent = this.getParent();
			if (parent instanceof ivy.UIBase) {
				let rect1 = parent.getSrcRect();
				if (rect1.x===0 && rect1.y===0 && rect1.width===0 && rect1.height===0) {

				} else {
					let pos22 = cc.p(rect1.x + rect1.width / 2, rect1.y + rect1.height / 2);
					rect.x -= pos22.x;
					rect.y -= pos22.y;
					rect.y = -rect.y;
					let p = ivy.Tools.ConvertPointFromGL(cc.p(rect));
					rect.x = p.x;
					rect.y = p.y;
				}
			}
		}

		this.setContentSize(size);
		if (this._aniPlayer) {
			this._aniPlayer.setScale(this._aniScaleX, this._aniScaleY);
		}
		switch (type) {
			case ivy.UIBase.eAnchorLT://ok
				if (this._aniPlayer)
					this._aniPlayer.setPosition(size.width/2.0, -size.height/2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x , rect.y)));
				if (this._backLayer)
					this._backLayer.setPosition(0, -size.height);
				break;
			case ivy.UIBase.eAnchorLB://ok
				if (this._aniPlayer)
					this._aniPlayer.setPosition(size.width / 2.0, size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x, rect.y + size.height)));
				if (this._backLayer)
					this._backLayer.setPosition(0, 0);
				break;
			case ivy.UIBase.eAnchorRT://ok
				if (this._aniPlayer)
					this._aniPlayer.setPosition(-size.width / 2.0, -size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width, rect.y)));
				if (this._backLayer)
					this._backLayer.setPosition(-size.width, -size.height);
				break;
			case ivy.UIBase.eAnchorRB://ok
				if (this._aniPlayer)
					this._aniPlayer.setPosition(-size.width / 2.0, size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width, rect.y + size.height)));
				if (this._backLayer)
					this._backLayer.setPosition(-size.width, 0);
				break;
			case ivy.UIBase.eAnchorHT://ok untest
				if (this._aniPlayer)
					this._aniPlayer.setPosition(0, -size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y)));
				if (this._backLayer)
					this._backLayer.setPosition(-size.width/2.0, -size.height);
				break;
			case ivy.UIBase.eANchorHB://ok untest
				if (this._aniPlayer)
					this._aniPlayer.setPosition(0, size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y + size.height)));
				if (this._backLayer)
					this._backLayer.setPosition(-size.width/2.0, 0);
				break;
			case ivy.UIBase.eAnchorLV://ok
				if (this._aniPlayer)
					this._aniPlayer.setPosition(size.width / 2.0, 0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x, rect.y+ size.height/2.0)));
				if (this._backLayer)
					this._backLayer.setPosition(0, -size.height/2.0);
				break;
			case ivy.UIBase.eAnchorRV://ok
				if (this._aniPlayer)
					this._aniPlayer.setPosition(-size.width / 2.0, 0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x+rect.width, rect.y + size.height / 2.0)));
				if (this._backLayer)
					this._backLayer.setPosition(-size.width, -size.height/2.0);
				break;
			case ivy.UIBase.eAnchorHV://ok
			default: {
				if (this._aniPlayer)
					this._aniPlayer.setPosition(0, 0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y + size.height / 2.0)));
				if (this._backLayer)
					this._backLayer.setPosition(-size.width/2.0, -size.height/2.0);
				break;
			}
		}
		this.setSrcPosition(this.getPosition());
	},
	reset : function() {
		if(this._aniPlayer) {
			let aniFileName = ivy.UIManager.getInstance().getAniFileNameByUIAniID(this._aniIDInUI1);
			let classID = ivy.SceneCommonDataManager.getInstance().getClassIDByAniFileName(aniFileName);
			this._aniPlayer.setPlayOverFunction(null);
			this._aniPlayer.resetAnimation(new ivy.AnimationID(classID, this._actionIDInUI1));
			this._setAniplayerPlayerState();
		}
	},
	getAniPlayer : function() {
		return this._aniPlayer;
	},
	setAniPlayer : function(ani) {
		this._aniPlayer = ani;
	},
	//程序调用接口 设置动画状态
	setAnimationIDByEditorAniID : function(id, state, endFunc=null) {
		let willChangeID = new ivy.AnimationID();
		switch (id) {
			case ivy.UIAniBox.EditorAniID.eID1:
				willChangeID = this._animationID1;
				break;
			case ivy.UIAniBox.EditorAniID.eID2:
				willChangeID = this._animationID2;
				break;
			case ivy.UIAniBox.EditorAniID.eID3:
				willChangeID = this._animationID3;
				break;
		}
		if (willChangeID.vaild()) {
			willChangeID.playState = state;
			this._aniPlayer.resetAnimation(willChangeID);
			if (state === ivy.AniPlayState.eNotCircle) {
				ivy.Tools.pushFunctionToCocosThread(function() {
					this._aniPlayer.setPlayOverFunction(endFunc);
				}.bind(this));
			}
		}
	},

	getAniScaleX : function() {
		return this._aniScaleX;
	},

	getAniScaleY : function() {
		return this._aniScaleY;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected
	_setAniplayerPlayerState : function() {
		if (this._aniPlayer) {
			switch (this._playType)
			{
				case ivy.UIAniBox.AniBoxPlayType.eCircle:
					this._aniPlayer.setPlayState(ivy.AniPlayState.eCircle);
					break;
				case ivy.UIAniBox.AniBoxPlayType.ePlayOnce:
					this._aniPlayer.setPlayState(ivy.AniPlayState.eNotCircle);
					break;
				case ivy.UIAniBox.AniBoxPlayType.ePlayOnceEndThenPlaySelAniCircle: {
					let aniNext = new ivy.AnimationID(this._aniIDInUI2, this._actionIDInUI2);
					this._aniPlayer.setPlayState(ivy.AniPlayState.eNotCircle);
					this._aniPlayer.setPlayOverFunction(function() {
						ivy.Tools.pushFunctionToCocosThread(function() {
							let aniFileName = ivy.UIManager.getInstance().getAniFileNameByUIAniID(this._aniIDInUI2);
							this.getAniPlayer().setPlayOverFunction(null);
							this.setAnimationID(aniFileName, this._actionIDInUI2, this._seqIDInUI2, true, ivy.AniPlayState.eCircle);
						}.bind(this));
					}.bind(this));
					break;
				}
				case ivy.UIAniBox.AniBoxPlayType.ePlayOnceEndThenPlaySelAniOnce: {
					this._aniPlayer.setPlayState(ivy.AniPlayState.eNotCircle);
					this._aniPlayer.setPlayOverFunction(function() {
						ivy.Tools.pushFunctionToCocosThread(function() {
							let aniFileName = ivy.UIManager.getInstance().getAniFileNameByUIAniID(this._aniIDInUI2);
							this.getAniPlayer().setPlayOverFunction(null);
							this.setAnimationID(aniFileName, this._actionIDInUI2, this._seqIDInUI2, true, ivy.AniPlayState.eNotCircle);
						}.bind(this));
					}.bind(this));
				}
					break;
				case ivy.UIAniBox.AniBoxPlayType.eHold:
					//暫時沒有需要
					break;
				default:
					break;
			}
		}
	},
	_aniPlayer : null,
	_playType : 0, //ivy.UIAniBox.AniBoxPlayType.eCircle;
	//////////////////////////////////////////////////////////////////////////
	_aniIDInUI1 : 0,
	_actionIDInUI1 : 0,
	_seqIDInUI1 : 0,
	_aniIDInUI2 : 0,
	_actionIDInUI2 : 0,
	_seqIDInUI2 : 0,
	_aniScaleX : 1.0,
	_aniScaleY : 1.0,

	_backLayer : null,

	_animationID1 : null,
	_animationID2 : null,
	_animationID3 : null,
});

ivy.UIAniBox.create = function(formName, blockName, sourceControlData) {
	return new ivy.UIAniBox(formName, blockName, sourceControlData);
};

ivy.UIAniBox.SwallowTouchType = {
	eNone       : 0,
	eAniBoxSize : 1,
	eAllScreen  : 2,
};

ivy.UIAniBox.AniBoxPlayType = {
	eCircle                             : 0,
	ePlayOnce                           : 1,
	ePlayOnceEndThenPlaySelAniCircle    : 2,
	ePlayOnceEndThenPlaySelAniOnce      : 3,
	eHold                               : 4,
};
ivy.UIAniBox.EditorAniID = {
	eID1    : 0,
	eID2    : 1,
	eID3    : 2,
};